package com.my.oschina.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.my.oschina.R;
import com.my.oschina.common.AppFragmentPagerAdapter;
import com.my.oschina.common.MyViewPager;
import com.my.oschina.fragment.TweetDetailCommentFragment;
import com.my.oschina.fragment.TweetDetailTweetFragment;

/**
 * 动弹详情，管理了FollowFragment、FansFragment2个Fragment
 */
public class TweetDetailActivity extends OscBaseActivity {

	private MyViewPager mViewPager = null;
	private int tweetId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewpager_layout);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setLogo(R.drawable.slm_action_tweet);
		setTitle(R.string.tweet_detail_head_title);

		// 获取新闻发布者、评论的评论者等的信息
		tweetId = getIntent().getIntExtra("tweet_id", 0);

		mViewPager = (MyViewPager) findViewById(R.id.viewpager);
		UserFriendPagerAdapter ufAdapter = new UserFriendPagerAdapter(
				getSupportFragmentManager());
		mViewPager.setOffscreenPageLimit(2);
		mViewPager.setAdapter(ufAdapter);
		mViewPager.setOnPageChangeListener(onPageChangeListener);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.addTab(actionBar.newTab().setText(getString(R.string.tweet))
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab()
				.setText(getString(R.string.comment))
				.setTabListener(tabListener));

	}

	/**
	 * TabListener
	 */
	ActionBar.TabListener tabListener = new ActionBar.TabListener() {

		@Override
		public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
			if (mViewPager.getCurrentItem() != tab.getPosition()) {
				mViewPager.setCurrentItem(tab.getPosition());
			}
		}

		@Override
		public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

		}

		@Override
		public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

		}
	};

	ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			getSupportActionBar().setSelectedNavigationItem(position);
		}
	};

	/**
	 * 动弹内容
	 */
	private Fragment getTweetContentFragment() {
		return getSupportFragmentManager().findFragmentByTag(
				TweetDetailTweetFragment.class.getName());
	}

	/**
	 * 动弹评论
	 */
	private Fragment getTweetCommentFragment() {
		return getSupportFragmentManager().findFragmentByTag(
				TweetDetailCommentFragment.class.getName());
	}

	/**
	 * 继承了AppFragmentPagerAdapter，后者又继承了PagerAdapter
	 */
	class UserFriendPagerAdapter extends AppFragmentPagerAdapter {

		List<Fragment> list = new ArrayList<Fragment>();

		public UserFriendPagerAdapter(FragmentManager fm) {
			super(fm);
			// 动弹内容
			if (getTweetContentFragment() == null) {
				list.add(TweetDetailTweetFragment.newInstance(tweetId));
			} else {
				list.add(getTweetContentFragment());
			}
			// 动弹评论
			if (getTweetCommentFragment() == null) {
				list.add(TweetDetailCommentFragment.newInstance(tweetId));
			} else {
				list.add(getTweetCommentFragment());
			}
		}

		@Override
		protected String getTag(int position) {
			List<String> tagList = new ArrayList<String>();
			tagList.add(TweetDetailTweetFragment.class.getName());
			tagList.add(TweetDetailCommentFragment.class.getName());
			return tagList.get(position);
		}

		@Override
		public Fragment getItem(int i) {
			return list.get(i);
		}

		@Override
		public int getCount() {
			return list.size();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_refresh, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			TweetDetailActivity.this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}